package com.token.controller;


import com.token.bean.Admin;
import com.token.service.AdminService;
import com.token.service.UserService;
import com.token.util.Respons.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-07-30
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;
    @Autowired
    UserService userService;

    @RequestMapping(value = "login.html",method = RequestMethod.GET)
    public String login(){
        return "login";
    }
    @RequestMapping(value = "index.html",method = RequestMethod.GET)
    public String index(){
        return "index";
    }
    @RequestMapping(value = "welcome.html",method = RequestMethod.GET)
    public String welcome(Model model){
        model.addAttribute("listt",userService.list());
        System.out.println("list:"+userService.list());
        System.out.println("model:"+model);
        return "welcome";
    }

    @RequestMapping(value = "/land")
    @ResponseBody
    public Map land(HttpSession session, String userName, String passWord) {
        if(adminService.login(userName,passWord,session)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getNoUser();
    }

    //获取手机验证码
    @RequestMapping("/SendSMS")
    @ResponseBody
    public Map SendSMS(Long phone) {
        return  adminService.SendSMS(phone);
    }

    //验证码校验  //// TODO: 2018/10/2 跳转页面model记得传手机号 用于设置缓存区
    @RequestMapping("/equalsCache")
    @ResponseBody
    public String equalsCache(String code, String phone, Admin admin,HttpSession session) {

        if(adminService.equalsCache(code,phone)==true){
            //获取系统当前时间
            //Date date=new Date();
            //在实体类中使用setCreateTime(date.getTime())方法就是在后台创建一个时间戳存到数据库中
            admin.setCreateTime(System.currentTimeMillis());
            if(adminService.save(admin)){
             Admin admin1 =  adminService.getById(admin.getId());
             if(admin1!=null){
                 session.setAttribute("user",admin1);
                 return "添加成功";
             }else{
                 return "添加失败";
             }
            }
            return "添加失败";
        }else {
            System.out.println("输入的验证码和发送的验证码不符,请重新核对验证码!");
            return "验证码不符";
        }

    }

}
